package mess;

public class LeeCode_67 {
    public static void main(String[] args) {
        System.out.println(addBinary("1010", "1011"));
    }
    private static String addBinary(String a, String b) {
        int len1 = a.length();
        int len2 = b.length();
        int count = Math.max(len1, len2);
        int temp = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            int index1 = a.length() - 1 - i;
            int index2 = b.length() - 1 - i;
            int n1 = index1 >= 0 ? a.charAt(index1) - 48 : 0;
            int n2 = index2 >= 0 ? b.charAt(index2) - 48 : 0;
            int sum = n1 + n2;

            int n3 = (sum + temp) % 2;
            sb.append(n3);
            temp = (sum + temp) / 2;

            if (i == count - 1 && temp > 0)
                sb.append(temp);
        }
        return sb.reverse().toString();
    }
}
